package com.k.demo.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.k.demo.entity.GoodOrder;
import com.k.demo.entity.Notice;
import com.k.demo.mapper.GoodOrderDetailMapper;
import com.k.demo.mapper.GoodOrderMapper;
import com.k.demo.service.GoodOrderService;


@Service
public class GoodOrderServiceImpl implements GoodOrderService{
	
	@Autowired
	private GoodOrderMapper goodOrederMapper;
	@Autowired
	private GoodOrderDetailMapper goodOrderDetailMapper;

	//查找全部订单的实现
	public List<GoodOrder> getAllGoodOrder() {
		// TODO Auto-generated method stub
		return goodOrederMapper.getAllGoodOrders();
	}

	//查找当前用户下全部订单
	public List<GoodOrder> getUserAllGoodOrders(Integer userid) {
		// TODO Auto-generated method stub
		return goodOrederMapper.getUserAllGoodOrders(userid);
	}

	//查找当前用户下全部已经已经送达订单
	public List<GoodOrder> getUserArrivedGoodOrders(Integer userid) {
		// TODO Auto-generated method stub
		return goodOrederMapper.getUserArrivedGoodOrders(userid);
	}

	//查找当前用户下全部已经支付订单
	public List<GoodOrder> getUserPaidGoodOrders(Integer userid) {
		// TODO Auto-generated method stub
		return goodOrederMapper.getUserPaidGoodOrders(userid);
	}

	//查找当前用户下全部已经删除订单
	public List<GoodOrder> getUserCancelGoodOrders(Integer userid) {
		// TODO Auto-generated method stub
		return goodOrederMapper.getUserCancelGoodOrders(userid);
	}

	//生成订单
	@Override
	public boolean insertGoodOrder(GoodOrder goodOrder) {
		// TODO Auto-generated method stub
		return goodOrederMapper.insertGoodOrder(goodOrder);
	}

	//获取id
	public GoodOrder getGoodOrderById(int orderid) {
		// TODO Auto-generated method stub
		return goodOrederMapper.getGoodOrderById(orderid);
	}

	//订单更新(所有更新包括状态在内的全部更新)
	public GoodOrder updateGoodOrder(GoodOrder goodorder) {
		
		int count = goodOrederMapper.updateGoodOrder(goodorder);
		
		if(count>0) {
			
			return goodorder;
			
		}
		
		return null;
	}

	//更新订单状态为已取消
	public GoodOrder updateGoodOrderState(int orderid) {
		
		int count = goodOrederMapper.updateGoodOrderState(orderid);
		
		if(count > 0) {
			
			return goodOrederMapper.getGoodOrderById(orderid);
			
		}
		
		return null;
	}

	//加载个人订单列表时状态更新
	public int updateState(int userid) {
		
		// TODO Auto-generated method stub
		return goodOrederMapper.updateGoodOrderByDate(userid);
	}

	//根据时间区间查询
	public List<GoodOrder> getByTimes(String time1, String time2) {
		// TODO Auto-generated method stub
		return goodOrederMapper.getGoodOrderByTime(time1, time2);
	}

	//批量删除
	public boolean deleteByGroup(List<Integer> list) {
		// TODO Auto-generated method stub
		return goodOrederMapper.deleteByGroup(list);
	}

	//根据id假删除
	public int deleteById(int id) {
		// TODO Auto-generated method stub
		return goodOrederMapper.falseDeleteById(id);
	}

	//根据时间进行区间的查询并分页显示结果
	public PageInfo<GoodOrder> getByTimeWithPage(int pageNum, int pageSize, String time1, String time2) {
		
		PageHelper.startPage(pageNum,pageSize);
		
		return new PageInfo<GoodOrder>(goodOrederMapper.getGoodOrderByTime(time1, time2));
	}

	//查询所有结果并分页显示
	public PageInfo<GoodOrder> getAllWithPage(int pageNum, int pageSize) {
		
		PageHelper.startPage(pageNum,pageSize);
		
		return new PageInfo<GoodOrder>(goodOrederMapper.getAllGoodOrders());
	}


	@Override
	public int getNumberByDate(String date) {
		// TODO Auto-generated method stub
		return goodOrederMapper.selectCountByDate(date);
	}



	//@返回查询结果
	public List<GoodOrder> getByGoodOrderInfo(GoodOrder goodOrder) {
		// TODO Auto-generated method stub
		return goodOrederMapper.getByGoodOrderInfo(goodOrder);
	}

	@Override
	public PageInfo<GoodOrder> getByGoodOrderInfoWith(int pageNum, int pageSize, GoodOrder goodOrder) {
		
		PageHelper.startPage(pageNum,pageSize);
		
		return new PageInfo<GoodOrder>(goodOrederMapper.getByGoodOrderInfo(goodOrder));
	}

	@Override
	public int getNumberToday() {
		// TODO Auto-generated method stub
		return goodOrederMapper.selectCountToday();
	}

	@Override
	public Double getTotalSales() {
		// TODO Auto-generated method stub
		return goodOrederMapper.selectTotalSales();
	}

	@Override
	public Double getActualSales() {
		// TODO Auto-generated method stub
		return goodOrederMapper.selectActualSales();
	}

	@Override
	public Double getTotalSalesByDate(String date) {
		// TODO Auto-generated method stub
		return goodOrederMapper.selectTotalSalesByDate(date);
	}

	@Override
	public Double getActualSalesByDate(String date) {
		// TODO Auto-generated method stub
		return goodOrederMapper.selectActualSalesByDate(date);
	}

	@Override
	public Map<String,Object> getDateAndTotalSaleNum(String start_date, String end_date) {
		// TODO Auto-generated method stub
		Map<String,Object> map = new HashMap<String,Object>();
		System.out.println(end_date);
		List<String> alldate = goodOrederMapper.getDate(start_date, end_date);
		List<String>  good_order= goodOrederMapper.selectByTime(start_date,end_date);
		List<Integer> salenum = new ArrayList<Integer> ();
		int[] dataArr = new int[alldate.size()];
		String[] dateArr = new String[alldate.size()];
		for(int i = 0;i<alldate.size();i++) {
			dataArr[i]=0;
			dateArr[i]=alldate.get(i);
			
		}
		
		if(good_order.size()!=0) {
			for (String i : good_order) {
				dataArr[alldate.indexOf(i)] = goodOrderDetailMapper.selectAllFoodNumberByDate(i);
			}
		}
	
		
		
		map.put("dataArr", dataArr);
		map.put("dateArr", dateArr);
		return map;
	}

	//返回挡墙用户的全部订单数量
	public int getAllOrderAmountByUserId(int user_id) {
		
		return goodOrederMapper.getAllOrderAmountByUserId(user_id);
	}

	//返回当前用户的未评价订单数量
	public int getNotEvaluationOrderAmountByUserId(int user_id) {
		// TODO Auto-generated method stub
		return goodOrederMapper.getNotEvaluationOrderAmountByUserId(user_id);
	}

	@Override
	public Map<String, Object> getDateAndOrderNum(String start_date, String end_date) {
		// TODO Auto-generated method stub
		List<String> date = goodOrederMapper.getDate(start_date, end_date);
		List<GoodOrder> ordernum = goodOrederMapper.selectCountByTime(start_date, end_date);
		Integer[]  data = new Integer[date.size()];
		for(int i =0;i<date.size();i++) {
			data[i]=0;
		}
		for(GoodOrder i : ordernum) {
			data[date.indexOf(i.getOrder_create_time())] = i.getPay_style();
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("date", date);
		map.put("data",data);
		
		return map;
	}

	@Override
	public Map<String, Object> getDateAndPrice(String start_date, String end_date) {
		// TODO Auto-generated method stub
		List<String> date = goodOrederMapper.getDate(start_date, end_date);
		List<GoodOrder> ordernum = goodOrederMapper.selectOriginAndActrulPriceByTime(start_date, end_date);
		Double[]  origin_price = new Double[date.size()];
		Double[]  true_price = new Double[date.size()];
		for(int i =0;i<date.size();i++) {
			origin_price[i]=0.0;
		    true_price[i]=0.0;
		}
		for(GoodOrder i : ordernum) {
			origin_price[date.indexOf(i.getOrder_create_time())] = i.getOrder_original_price();
			true_price[date.indexOf(i.getOrder_create_time())] = i.getOrder_true_price();
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("date", date);
		map.put("origin_price", origin_price);
		map.put("true_price",true_price);
		
		return map;

	}

}
